package com.guo.pet_store.admin.controller;

import com.guo.pet_store.admin.pojo.ShopCar;
import com.guo.pet_store.admin.pojo.User;
import com.guo.pet_store.admin.service.ShopService;
import com.guo.pet_store.admin.service.UploadService;
import com.guo.pet_store.admin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
@RequestMapping("/admin")
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    ShopService shopService;
    @Autowired
    UploadService uploadService;
    @GetMapping("/users")
    public String getUsers(Model model, @PageableDefault(size = 20) Pageable pageable){
         model.addAttribute("page",userService.listUser(pageable));
         return "admin/users";
    }
    @GetMapping("/userInput")
    public String getUserInput(){
        return "admin/user-input";
    }
//    @PostMapping("/addUser")
//    public String addUser(User user, RedirectAttributes redirectAttributes){
//        System.out.println("==========================================");
//        if(user.getEditor()==null){
//            user.setEditor(false);
//        }
//        //设置时间
//        user.setCreateDate(new Date());
//        User user1 = userService.addUser(user);
//        System.out.println(user);
//        if(user1 != null){
//            redirectAttributes.addFlashAttribute("message","添加失败");
//        }else {
//            redirectAttributes.addFlashAttribute("message","添加成功");
//        }
//        return "redirect:/admin/users";
//    }
@PostMapping("/addUser")
public String register(User user, Model model, @RequestParam("picture") MultipartFile multipartFile){

    User userByName = userService.getUserByName(user.getUsername());
    //判断用户名是否存在
    if(userByName != null){
        model.addAttribute("message","该用户名已存在");
        return "admin/user-input";
    }else {
        //上传图片
        String imagePath = uploadService.uploadImage(multipartFile);
        user.setAvatarPath(imagePath);
        if(user.getMoney() == null){
            user.setMoney(0.0);
        }
        user.setEditor(false);
        user.setCreateDate(new Date());
        userService.addUser(user);
        User addedUser = userService.getUserByName(user.getUsername());
        //与购物车关联
        shopService.connectToUser(addedUser.getId());
        ShopCar shopCarByUser = shopService.findShopCarByUser(addedUser);

        user.setShopCar(shopCarByUser);
        userService.updateUser(user,user.getId());
        model.addAttribute("message","添加成功");
        return "redirect:/admin/users";
    }
}
}
